/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.myapp.struts.Carrera;

import BD.BaseDatos;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 *
 * @author Andreina Wilhelm
 */
public class DesactivarCarreraAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        // crea instancia de BD
        BaseDatos bd = new BaseDatos();

        // Mensaje de exito o fracaso
        ActionMessages msj = new ActionMessages();

        try {
            // establece conexion con BD
            bd.establecerConexion();

            // Se obtiene de la sesion la carrera seleccionada
            HttpSession sesion = request.getSession();
            ConsultarCarreraForm ccf = (ConsultarCarreraForm) sesion.getAttribute("ConsultarCarreraForm");
            Carrera car = ccf.getCarreraSelected();

            // se crea el query
            String query = "UPDATE carrera SET (activo) = ('false') WHERE  codigo='" + car.getCodigo() + "' AND idInstitucion='" + car.getIdInstitucion()+ "'";

            // se ejecuta query
            boolean result = bd.updateQuery(query);

            // Se termina conexion con BD
            bd.terminarConexion();

            // si no hay problema con el query ejecutado
            if (result) {

                // Se colocar la carrera seleccionada igual a null ya que se acaba de desactivar
                ccf.setCarreraSelected(null);
                sesion.setAttribute("ConsultarCarreraForm", ccf);

                // mensaje de exito
                msj.add("ok_op", new ActionMessage("ok.op", "Carrera", "desactivada"));
                saveMessages(request, msj);

                return mapping.findForward(SUCCESS);
            }

        } catch (Exception e) {
            System.out.println("ERROR: Problema al borrar de la BD");
        }

        // Se termina conexion con BD
        bd.terminarConexion();
            
        // mensaje de fracaso
        msj.add("err_op", new ActionMessage("errors.op", "Carrera", "desactivada"));
        saveErrors(request, msj);

        // in other cases, return login page
        return mapping.getInputForward();
    }
}
